﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }
        
        //程序加载时打开数据库
        private void Register_Load(object sender, EventArgs e)
        {
            //加载皮肤
            this.skinEngine1.Active = true;
            this.skinEngine1.SkinFile = "skin/Page.ssk";
        }
        //确认注册
        private void btn_ConReg_Click(object sender, EventArgs e)
        {
            //获取用户输入的用户名
            string userName = this.txt_Username.Text.Trim();
            //获取用户输入的密码
            string pwd = this.txt_Pwd.Text.Trim();
            //获取用户再次输入的密码
            string conPwd = this.txt_ConPwd.Text.Trim();
            //判断用户名是否存在
            var exUserName = string.Format("select Username from Users where UserName='{0}'", userName);
            var dt = DbHelper.GetSelect(exUserName);
            //判断用户输入的内容是否为null
            var isUserName = string.IsNullOrEmpty(userName);
            var isPwd = string.IsNullOrEmpty(pwd);
            var isConPwd = string.IsNullOrEmpty(conPwd);
            if (!(isUserName && isPwd && isConPwd) && pwd.Equals(conPwd) && dt.Rows.Count==0)
            {
                string sql = string.Format("insert into Users(Username,Password) values('{0}','{1}')",userName,pwd);
                var result = DbHelper.GetIDU(sql);
                if (result>=1)
                {
                    MessageBox.Show("注册成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    Main main = new Main();
                    main.Show();
                } 
            }
            else if (dt.Rows.Count !=0)
            {
                MessageBox.Show("用户名已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if(isUserName)
            {
                MessageBox.Show("用户名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (isPwd)
            {
                MessageBox.Show("密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (isConPwd)
            {
                MessageBox.Show("确认密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (pwd!=conPwd)
            {
                MessageBox.Show("两次密码不一致！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //else
            //{
            //    MessageBox.Show("您未填写任何内容！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
        }
        //取消
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            Main main = new Main();
            main.Show();
        }
    }
}
